package org.example.sunrise.sprites.plants;

import javafx.scene.image.Image;
import lombok.Setter;
import org.example.sunrise.sprites.Sprite;
import org.example.sunrise.utils.ImageUtil;

import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.stream.IntStream;

public class Plant extends Sprite {
    protected static final List<Image> peashooterImages = IntStream.rangeClosed(0, 12).mapToObj(i -> ImageUtil.createImage(String.format("peashooter/Peashooter_%d.png", i), 0, 0, Plant.class)).toList();
    protected static final List<Image> sunflowerImages = IntStream.rangeClosed(0, 17).mapToObj(i -> ImageUtil.createImage(String.format("sunflower/SunFlower_%d.png", i), 0, 0, Plant.class)).toList();

    private final List<Image> images;
    private int index = 0;
    @Setter
    private ScheduledFuture<?> future;

    public Plant(List<Image> images) {
        super(images.getFirst());
        this.images = images;
    }

    @Override
    public void switchImage() {
        setImage(images.get(index));
        index = (index + 1) % images.size();
    }

    @Override
    public void release() {
        if (future != null) {
            future.cancel(true);
            future = null;
        }
    }
}
